<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/

class c_main_class extends AWS_CONTROLLER
{

	public function get_access_rule()
	{
		$rule_action["rule_type"] = "black"; //黑名单,黑名单中的检查  'white'白名单,白名单以外的检查
		$rule_action["guest"] = array();
		$rule_action["user"] = array();
		
		return $rule_action;
	}

	public function setup()
	{
		$this->per_page = get_setting('contents_per_page');
		
		$this->crumb('话题', '/topic/');
	}

	public function hot_action()
	{
		$content_category = $this->model('module')->content_category();
			
		TPL::assign('content_category', $content_category);
		
		if ($_GET['category'] AND $category_info = $this->model('system')->get_category_info($_GET['category']))
		{
			TPL::assign('category_info', $category_info);
				
		}
		
		$topics = $this->model('topic')->get_hot_topic($_GET['category'], 0, calc_page_limit($_GET['page'], 12));
		
		$this->model('pagination')->initialize(array(
			'base_url' => get_js_url('/topic/hot/category-'.$_GET['category']), 
			'total_rows' => $topics['topics_count'], 
			'per_page' => $this->per_page
		));
		
		TPL::assign('topics', $topics['topics']);
		TPL::assign("pagination", $this->model('pagination')->create_links());
		
		$this->crumb('话题广场 - 热门话题', '/topic/hot/');
		
		TPL::import_css(array(
			'css/discussion.css'
		));
		
		TPL::output('topic/square');
	}

	public function new_action()
	{
		$content_category = $this->model('module')->content_category();
			
		TPL::assign('content_category', $content_category);
		
		if ($_GET['category'] AND $category_info = $this->model('system')->get_category_info($_GET['category']))
		{
			TPL::assign('category_info', $category_info);
		
		}
		
		if($_GET['category'])
		{
			$topics_count = $this->model('topic')->get_category_new_topic($_GET['category'],'add_time>' . (time() - (60 * 60 * 24 * 30)), calc_page_limit($_GET['page'], 12),true);
			$topics = $this->model('topic')->get_category_new_topic($_GET['category'],'add_time>' . (time() - (60 * 60 * 24 * 30)), calc_page_limit($_GET['page'], 12),false);			
		}
		else		
		{
			$topics_count = $this->model('topic')->get_topic_list('add_time > ' . (time() - (60 * 60 * 24 * 30)), null, true);			
			$topics = $this->model('topic')->get_topic_list('add_time>' . (time() - (60 * 60 * 24 * 30)), calc_page_limit($_GET['page'], 12));				
		}
		
		
		$this->model('pagination')->initialize(array(
			'base_url' => get_js_url('/topic/new/category-'.$_GET['category']), 
			'total_rows' => $topics_count, 
			'per_page' => $this->per_page
		));
		
		TPL::assign('topics', $topics);
		TPL::assign("pagination", $this->model('pagination')->create_links());
		
		$this->crumb('话题广场 - 最新话题', '/topic/new/');
		
		TPL::import_css(array(
			'css/discussion.css'
		));
		
		TPL::output('topic/square');
	}

	public function category_action()
	{
		if ($content_category = $this->model('module')->content_category())
		{
			foreach ($content_category as $key => $val)
			{
				$content_category[$key]['topics'] = $this->model('topic')->get_category_topic($val['id'], 0, null);
			}
		}
		TPL::assign('content_category', $content_category);
		
		$this->crumb('话题广场 - 分类话题', '/topic/category/');
		
		TPL::import_css(array(
			'css/discussion.css'
		));
		
		TPL::output('topic/square');
	}

	public function index_action()
	{
		if ($_GET['id'] or $_GET['title'])
		{
			$this->topic_action();
		}
		else
		{
			$this->hot_action();
		}
	}

	
	public function topic_action()
	{
		$uid = $this->user_id;
		$topic_id = $_GET['id'];
		$topic_title = trim($_GET["title"]);
		$data = array();
		$tmp = array();
		$quesion_id_list = array();
		$focus_question = array();
		$answer_id_list = array();
		$account_obj = $this->model('account');
		$answer_obj = $this->model('answer');
		$question_obj = $this->model('question');
		$index_obj = $this->model('index');
		
		if ($topic_title)
		{
			$topic_id = $this->model('topic')->get_topic_id_by_title($topic_title, get_setting('cache_level_high')); //60秒缓存
			

			if ($topic_id > 0)
			{
				$topic_info = $this->data_cache(ZCACHE::format_key('topic_get_topic_by_title_' . base64_encode($topic_title)), '$this->model("topic")->get_topic_by_title("' . $topic_title . '")', get_setting('cache_level_high'), ZCACHE::format_key("topic_info_" . $topic_id));
			}
		}
		else if ($topic_id)
		{
			$topic_info = $this->model('topic')->get_topic_by_title($topic_id);
			
			if (! $topic_info) //避免有时 topic title 是全数字
			{
				$topic_info = $this->model('topic')->get_topic($topic_id);
			}
		}
		
		if (empty($topic_info))
		{
			H::js_pop_msg('话题不存在', '/');
		}
		
		$topic_id = $topic_info['topic_id'];
		
		$best_answer = $this->model('topic')->get_best_answer_list($topic_info['topic_id'], $this->user_id);
		
		if ($best_answer)
		{
			$best_answer = array_slice($best_answer, 0, 5);
			
			TPL::assign("best_answer", $best_answer);
		}
		//p($best_answer);
		$no_answer_question_count = $question_obj->get_question_info($topic_info['topic_id'], "", 0, true, null, null, 0);
		
		if (empty($topic_info['topic_pic']))
		{
			$topic_info['topic_pic_max'] = G_STATIC_URL . '/common/topic-max-img.jpg';
		}
		else
		{
			$topic_info['topic_pic_max'] = get_setting('upload_url') . '/topic/' . str_replace(AWS_APP::config()->get('image')->topic_thumbnail['min']['w'] . '_' . AWS_APP::config()->get('image')->topic_thumbnail['min']['h'], AWS_APP::config()->get('image')->topic_thumbnail['max']['w'] . '_' . AWS_APP::config()->get('image')->topic_thumbnail['max']['h'], $topic_info['topic_pic']);
		}
		
		$topic_info['has_focus'] = $this->model('topic')->has_focus_topic($this->user_id, $topic_info['topic_id']);
		$topic_info['topic_description'] = FORMAT::parse_links($topic_info['topic_description']);
		$topic_info['topic_experience'] = $this->model('topic')->get_topic_experience($uid, $topic_id);
		$topic_parent_list = $this->model('topic')->get_topic_parent_id($topic_id);		
		
		if ($topic_parent_list)
		{
			foreach ($topic_parent_list as $key => $val)
			{
				$parent[] = $val["topic_id"];
			}
		}
		
		$best_question_count = $this->model('topic')->get_all_best_question_by_topic($topic_info['topic_id'], "", true);
		
		
		TPL::assign("topic_parent_list", $topic_parent_list);
		TPL::assign("topic_info", $topic_info);
		TPL::assign("no_answer_question_count", $no_answer_question_count);
		TPL::assign("best_question_count", $best_question_count);
		
		
		//相关话题	
		//$topic_othter_child_list = $this->model('topic')->get_topic_childs($topic_info["parent_id"], "0,10");
		//$topic_othter_child_list = $this->model('topic')->get_topics_child_id($parent, " tp.topic_id<>'{$topic_id}' ");
		
		
	
		if ($questions_rs = $this->model('topic')->get_question_ids_by_topic($topic_id))
		{
			foreach ($questions_rs as $key => $val)
			{
				$questions_topic[] = $val['question_id'];
			}
		}
		if ($questions_topic)
		{
			$topic_othter_child_list = $this->model('question_topic')->get_question_topic_by_question($questions_topic, 12);
		}
		
		if(is_array($topic_othter_child_list))
		{
			foreach($topic_othter_child_list as $key=>$val)
			{
				if($val['topic_id']==$topic_id)
				{
					unset($topic_othter_child_list[$key]);
				}
			}
		}
		else		
		{
			$topic_othter_child_list = array();
		}
		
		$this->crumb($topic_info['topic_title'], '/topic/' . $topic_info['topic_title']);
		
		if($topic_info['topic_description'])
		{
		TPL::set_meta('description', $topic_info['topic_title'] . ' - ' . cjk_substr(str_replace("\r\n", ' ', strip_tags($topic_info['topic_description'])), 128));
		}
	
		TPL::assign("topic_othter_child_list", $topic_othter_child_list);
		
		TPL::import_css(array(
			'css/discussion.css'
		));
		
		TPL::import_js(array(
			'js/ajaxupload.js'
		));
		
		TPL::output('topic/index');
	
	}

	/**
	 * 话题操作日志 
	 */
	public function log_action()
	{
		$uid = $this->user_id;
		$topic_id = intval($_GET['topic_id']);
		$topic_title = trim($_GET["topic_title"]);
		$data = array();
		$tmp = array();
		$quesion_id_list = array();
		$focus_question = array();
		$answer_id_list = array();
		$account_obj = $this->model('account');
		$answer_obj = $this->model('answer');
		$question_obj = $this->model('question');
		$index_obj = $this->model('index');
		
		if ($topic_title)
		{
			$topic_id = $this->model('topic')->get_topic_id_by_title($topic_title, get_setting('cache_level_high')); //60秒缓存
			

			if ($topic_id > 0)
			{
				$topic_info = $this->data_cache(ZCACHE::format_key('topic_get_topic_by_title_' . base64_encode($topic_title)), '$this->model("topic")->get_topic_by_title("' . $topic_title . '")', get_setting('cache_level_high'), ZCACHE::format_key("topic_info_" . $topic_id));
			}
		}
		else if ($topic_id)
		{
			$topic_info = $this->model('topic')->get_topic_by_title($topic_id);
			
			if (! $topic_info) //避免有时 topic title 是全数字
			{
				$topic_info = $this->model('topic')->get_topic($topic_id);
			}
		}
		
		if (empty($topic_info))
		{
			H::js_pop_msg('话题不存在', '/');
		}
		
		$no_answer_question_count = $question_obj->get_question_info($topic_info['topic_id'], "", 0, true, null, null, 0);
		
		if (empty($topic_info['topic_pic']))
		{
			$topic_info['topic_pic_max'] = G_STATIC_URL . '/common/topic-max-img.jpg';
		}
		else
		{
			$topic_info['topic_pic_max'] = get_setting('upload_url') . '/topic/' . str_replace(AWS_APP::config()->get('image')->topic_thumbnail['min']['w'] . '_' . AWS_APP::config()->get('image')->topic_thumbnail['min']['h'], AWS_APP::config()->get('image')->topic_thumbnail['max']['w'] . '_' . AWS_APP::config()->get('image')->topic_thumbnail['max']['h'], $topic_info['topic_pic']);
		}
		
		$topic_info['has_focus'] = $this->model('topic')->has_focus_topic($this->user_id, $topic_info['topic_id']);
		$topic_info['topic_description'] = $topic_info['topic_description'];
		$topic_info['topic_experience'] = $this->model('topic')->get_topic_experience($uid, $topic_id);
		$topic_parent_list = $this->model('topic')->get_topic_parent_id($topic_id);		
		$best_answer = $this->model('topic')->get_best_answer_list($topic_info['topic_id'], $this->user_id);
		
		if ($best_answer)
		{
			$best_answer = array_slice($best_answer, 0, 5);
			
			TPL::assign("best_answer", $best_answer);
		}
		
		if ($topic_parent_list)
		{
			foreach ($topic_parent_list as $key => $val)
			{
				$parent[] = $val["topic_id"];
			}
		}
		
		TPL::assign("topic_parent_list", $topic_parent_list);
		TPL::assign("topic_info", $topic_info);
		TPL::assign("no_answer_question_count", $no_answer_question_count);		
		TPL::assign("page_title", '话题: ' . $topic_info['topic_title']);
		//相关话题	
		//$topic_othter_child_list = $this->model('topic')->get_topic_childs($topic_info["parent_id"], "0,10");
		//$topic_othter_child_list = $this->model('topic')->get_topics_child_id($parent, " tp.topic_id<>'{$topic_id}' ");
		

		if($topic_info['topic_description'])
		{
			TPL::set_meta('description', $topic_info['topic_title'] . ' - ' . cjk_substr(str_replace("\r\n", ' ', strip_tags($topic_info['topic_description'])), 128));
		}
		
		
		if ($questions_rs = $this->model('topic')->get_question_ids_by_topic($topic_id))
		{
			foreach ($questions_rs as $key => $val)
			{
				$questions_topic[] = $val['question_id'];
			}
		}
		if ($questions_topic)
		{
			$topic_othter_child_list = $this->model('question_topic')->get_question_topic_by_question($questions_topic, 12);
		}
		
		if (! is_array($topic_othter_child_list))
		{
			$topic_othter_child_list = array();
		}
		
		TPL::assign("topic_othter_child_list", $topic_othter_child_list);
		
		TPL::import_css(array(
			'css/discussion.css'
		));
		
		TPL::import_js(array(
			'js/ajaxupload.js'
		));
		
		TPL::output('topic/log');
	}
}